

<template>
  <div>
    <el-card>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="roleName" label="角色名称">
        </el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button type="primary" round @click="handleEdit(scope.row)">编辑</el-button>
            <el-popconfirm title="你确定要删除吗" confirm-button-text="确认" cancel-button-text="取消"
              @confirm="handleDelete(scope.row)">
              <template #reference>
                <el-button type="danger" round>删除</el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog v-model="dialogVisible" title="更新角色" @close="confirm" style="border-radius: 20px; font-weight: bold;">
      <el-form :model="updateForm">
        <el-form-item label="角色名称: " prop="roleName">
          <el-input v-model="updateForm.roleName" autocomplete="on" />
        </el-form-item>
        <el-form-item label="角色权限: " prop="rights">
          <el-tree :data="rightsList" show-checkbox node-key="path" :props="{label:'title'}" ref="treeRef" :check-strictly="true"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="confirm">
            确认
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script setup>
import axios from 'axios';
import { ref, onMounted, reactive, nextTick} from 'vue'
//ref在dom上能拿到节点对象，在组件上能拿到组件的实力对象(先设置为null)
const treeRef = ref(null)
const tableData = ref([])
//权限列表
const rightsList = ref([])
//记录发送给后端的数据
const updateList = ref({})
const dialogVisible = ref(false)
const updateForm = reactive({
  roleName: '',
  rights: []
})
onMounted(() => {
  getList()
  getRolesList()
})
const handleDelete = async (item) => {
await axios.delete(`/admin/roles/${item._id}`)
await getList()
}
const handleEdit = (item) => {
  console.log('item',item.rights);
  dialogVisible.value = true
  updateForm.roleName = item.roleName
  updateForm.rights = item.rights
  updateList.value = item
  //nextTick函数会等上面的状态执行完才会执行，一次性使用
  nextTick(()=>{
    //el-tree的对象操作js的方法
    treeRef.value.setCheckedKeys(updateForm.rights)
  })
}
const getList = async () => {
  const res = await axios.get("/admin/roles")
  // console.log('roles', res.data);
  tableData.value = res.data
}
const getRolesList = async () => {
  const res = await axios.get("/admin/rights")
  // console.log('roles', res.data);
  rightsList.value = res.data
}
const confirm = async () => {
  dialogVisible.value = false
  //更新后台权限
  // console.log(updateList.value._id);
  // console.log(updateForm.roleName);
  // console.log(treeRef.value.getCheckedKeys());
  await axios.put(`/admin/roles/${updateList.value._id}`,{
    roleName:updateForm.roleName,
    rights:treeRef.value.getCheckedKeys()
  })
  await getList()
}
</script>
<style scoped lang='scss'>
.el-card {
  // background-color: aqua;
  border-radius: 30px;

  :deep(.el-table .cell) {
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 18px;
    font-weight: bold;
  }
}
</style>